package cn.com.cennavi.fileMake;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

import cn.com.cennavi.bean.DifPointRoadGeo;
import cn.com.cennavi.util.MapUtil;

/**
 * 
 * @ClassName: DifPointRoadGeoService
 * @Description: TODO（这里用一句话描述这个类的作用）
 * @author bull
 * @date 2016年3月21日 下午5:28:19
 *
 */
public class DifPointRoadGeoService {
	private static String difPointRoadFile = "D:\\data\\lonlat_16G.csv";
	private static String R_G_Map_File = "D:\\data\\R-G\\R-G_16map.csv";
	private static  String bufferAreaDistance="0.00015";
	private static HashMap<String, DifPointRoadGeo> difPointRoadGeo = new HashMap<>();
	private static HashMap<String, Geometry> difPointRoadBuffer = new HashMap<>();

	 private static HashMap<String, DifPointRoadGeo> difPointStartRoadGeo =new HashMap<>();
	private static HashMap<String, Geometry> difPointStartRoadBuffer = new HashMap<>();

	 private static HashMap<String, DifPointRoadGeo> difPointEndRoadGeo = new
	 HashMap<>();
	private static HashMap<String, Geometry> difPointEndRoadBuffer = new HashMap<>();

	static {
		if (difPointRoadBuffer.size() == 0) {
			/**
			 * 得到分歧点道路的形状
			 */
			difPointRoadGeo = getDifPointLinkGeo(difPointRoadFile, R_G_Map_File);
			/**
			 * 得到每个分歧点道路的缓冲区
			 */
			difPointRoadBuffer = getDifPointBuffer(difPointRoadGeo, Double.parseDouble(bufferAreaDistance));
		}
	}

	public static void main(String[] args) {
		DifPointRoadGeoService dd = new DifPointRoadGeoService();
	}

	public static HashMap<String, Geometry> getDifPointRoadBuffer(String difPointRoadFile,String R_G_Map_File) {
		difPointRoadGeo = getDifPointLinkGeo(difPointRoadFile, R_G_Map_File);
		/**
		 * 得到每个分歧点道路的缓冲区
		 */
		difPointRoadBuffer = getDifPointBuffer(difPointRoadGeo, Double.parseDouble(bufferAreaDistance));
		return difPointRoadBuffer;
	}

	private static HashMap<String, Geometry> getDifPointEndBuffer(HashMap<String, DifPointRoadGeo> difPointRoadGeo, double bufferLength) {
		// TODO Auto-generated method stub
		HashMap<String, Geometry> point_dirToBufferMap = new HashMap<>();
		for (Map.Entry<String, DifPointRoadGeo> entry : difPointRoadGeo.entrySet()) {
			DifPointRoadGeo dpr = entry.getValue();
			try {
				String key = entry.getKey();
				Geometry g1 = new WKTReader().read(dpr.getGeoString());
//				if (dpr.getGeoString().trim().equals(
//						"LINESTRING (116.38106 39.97125, 116.38116 39.97223, 116.38118 39.97245, 116.38118 39.97259, 116.38119 39.97266 ,116.38119 39.97266, 116.3812 39.97323, 116.38119 39.97347, 116.38117 39.97378 ,116.38117 39.97378, 116.38124 39.97441, 116.38139 39.97566, 116.38143 39.97604, 116.38145 39.97632, 116.38145 39.97657, 116.38145 39.97674, 116.38145 39.9768, 116.38145 39.97682, 116.38144 39.97685, 116.38142 39.97717, 116.38142 39.9772, 116.38125 39.97814, 116.38124 39.97817, 116.38108 39.97878, 116.38077 39.9799)")) {
//					// System.out.println("asdasd");
//				}
				Geometry buffer = g1.buffer(bufferLength);
				point_dirToBufferMap.put(key, buffer);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return point_dirToBufferMap;
	}

	private static HashMap<String, Geometry> getDifPointStartBuffer(HashMap<String, DifPointRoadGeo> difPointRoadGeo, double bufferLength) {
		// TODO Auto-generated method stub
		HashMap<String, Geometry> point_dirToBufferMap = new HashMap<>();
		for (Map.Entry<String, DifPointRoadGeo> entry : difPointRoadGeo.entrySet()) {
			DifPointRoadGeo dpr = entry.getValue();
			try {
				String key = entry.getKey();
				Geometry g1 = new WKTReader().read(dpr.getGeoString());
				Geometry buffer = g1.buffer(bufferLength);
				point_dirToBufferMap.put(key, buffer);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return point_dirToBufferMap;
	}

	public static HashMap<String, Geometry> getDifPointStartRoadBuffer() {

		return difPointStartRoadBuffer;
	}

	public static HashMap<String, Geometry> getDifPointEndRoadBuffer() {

		return difPointEndRoadBuffer;
	}

//	private static HashMap<String, DifPointRoadGeo> getDifPointEndRoadGeo(String difPointLinkMapFile, String allLinkMap, String outFile) {
//		// TODO Auto-generated method stub
//		HashMap<String, DifPointRoadGeo> difPointRoadGeo = new HashMap<>();
//		HashMap<Integer, String> linkGeo = new HashMap<>();
//
//		try {
//			BufferedReader br = new BufferedReader(new FileReader(allLinkMap));
//			BufferedWriter write = new BufferedWriter(new FileWriter(outFile, true));
//			String line = "";
//			while ((line = br.readLine()) != null) {
//				String[] mapData = line.split("\\:");
//				int linkId = Integer.parseInt(mapData[0]);
//				String lonlat = mapData[9].replace("LINESTRING (", "").replace(")", "").trim();
//				linkGeo.put(linkId, lonlat);
//			}
//			BufferedReader br1 = new BufferedReader(new FileReader(difPointLinkMapFile));
//			String line1 = "";
//			while ((line1 = br1.readLine()) != null) {
//
//				DifPointRoadGeo prg = new DifPointRoadGeo();
//
//				String[] mapData = line1.split(",");
//				String difPointId = mapData[6];
//				String dir = mapData[10];
//				String key = difPointId + "_" + dir;
//				prg.setDifPointId(difPointId);
//				prg.setDir(mapData[10]);
//				String[] linkSeq = mapData[1].split("\\|");
//				prg.setLinkSeq(mapData[1]);
//				StringBuffer sb = new StringBuffer();
//				sb.append("LINESTRING (");
//				for (int i = linkSeq.length - 1; i < linkSeq.length; i++) {
//
//					String geo = linkGeo.get(Integer.parseInt(linkSeq[i]));
//					if (geo != null) {
//						sb.append(geo);
//					} else {
//						break;
//					}
//
//				}
//				sb.append(")");
//				prg.setGeoString(sb.toString());
//				difPointRoadGeo.put(key, prg);
//				write.write(prg.toString());
//				write.newLine();
//				write.flush();
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		return difPointRoadGeo;
//	}
//
//	private static HashMap<String, DifPointRoadGeo> getDifPointStartRoadGeo(String difPointLinkMapFile, String allLinkMap, String outFile) {
//		// TODO Auto-generated method stub
//		HashMap<String, DifPointRoadGeo> difPointRoadGeo = new HashMap<>();
//		HashMap<Integer, String> linkGeo = new HashMap<>();
//
//		try {
//			BufferedReader br = new BufferedReader(new FileReader(allLinkMap));
//			BufferedWriter write = new BufferedWriter(new FileWriter(outFile, true));
//			String line = "";
//			while ((line = br.readLine()) != null) {
//				String[] mapData = line.split("\\:");
//				int linkId = Integer.parseInt(mapData[0]);
//				String lonlat = mapData[9].replace("LINESTRING (", "").replace(")", "").trim();
//				linkGeo.put(linkId, lonlat);
//			}
//			BufferedReader br1 = new BufferedReader(new FileReader(difPointLinkMapFile));
//			String line1 = "";
//			while ((line1 = br1.readLine()) != null) {
//
//				DifPointRoadGeo prg = new DifPointRoadGeo();
//
//				String[] mapData = line1.split(",");
//				String difPointId = mapData[6];
//				String dir = mapData[10];
//				String key = difPointId + "_" + dir;
//				prg.setDifPointId(difPointId);
//				prg.setDir(mapData[10]);
//				String[] linkSeq = mapData[1].split("\\|");
//				prg.setLinkSeq(mapData[1]);
//				StringBuffer sb = new StringBuffer();
//				sb.append("LINESTRING (");
//				for (int i = 0; i < 1; i++) {
//
//					String geo = linkGeo.get(Integer.parseInt(linkSeq[i]));
//					if (geo != null) {
//						sb.append(geo);
//					} else {
//						break;
//					}
//
//				}
//				sb.append(")");
//				prg.setGeoString(sb.toString());
//				difPointRoadGeo.put(key, prg);
//				write.write(prg.toString());
//				write.newLine();
//				write.flush();
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		return difPointRoadGeo;
//	}

	public static HashMap<String, DifPointRoadGeo> getDifPointRoadGeo() {

		return difPointRoadGeo;
	}

	/**
	 * 根据北京地图mif\mid数据和分歧点link序列得到,分歧点link序列的形状
	 */
	private static HashMap<String, DifPointRoadGeo> getDifPointLinkGeo(String difPointLinkMapFile, String allLinkMap) {
		HashMap<String, DifPointRoadGeo> difPointRoadGeo = new HashMap<>();
		HashMap<Integer, String> linkGeo = new HashMap<>();

		try {
			BufferedReader br = new BufferedReader(new FileReader(allLinkMap));
			// BufferedWriter write = new BufferedWriter(new FileWriter(outFile,
			// true));
			String line = "";
			while ((line = br.readLine()) != null) {
				String[] mapData = line.split("\\:");
				int linkId = Integer.parseInt(mapData[0]);
				String lonlat = mapData[9].replace("LINESTRING (", "").replace(")", "").trim();
				linkGeo.put(linkId, lonlat);
			}
			BufferedReader br1 = new BufferedReader(new FileReader(difPointLinkMapFile));
			String line1 = "";
			while ((line1 = br1.readLine()) != null) {

				DifPointRoadGeo prg = new DifPointRoadGeo();

				String[] mapData = line1.split("\\:");
				String difPointId = mapData[1];
				String angle=mapData[7];
				String dir = mapData[9];
				String key = difPointId + "_" + dir+"_"+angle;
				prg.setDifPointId(difPointId);
				prg.setDir(mapData[10]);
				String[] linkSeq = mapData[1].split("\\|");
				prg.setLinkSeq(mapData[1]);
				StringBuffer sb = new StringBuffer();
				sb.append("LINESTRING (");
				for (int i = 0; i < linkSeq.length; i++) {

					String geo = linkGeo.get(Integer.parseInt(linkSeq[i]));
					if (geo != null) {
						if (i != 0) {
							sb.append(",");
						}
						sb.append(geo);
					} else {
						break;
					}

				}
				sb.append(")");
				prg.setGeoString(sb.toString());
				difPointRoadGeo.put(key, prg);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return difPointRoadGeo;

	}

	private static HashMap<String, Geometry> getDifPointBuffer(HashMap<String, DifPointRoadGeo> difPointRoadGeo, double bufferLength) {
		// TODO Auto-generated method stub
		HashMap<String, Geometry> point_dirToBufferMap = new HashMap<>();
		for (Map.Entry<String, DifPointRoadGeo> entry : difPointRoadGeo.entrySet()) {
			DifPointRoadGeo dpr = entry.getValue();
			try {
				String key = entry.getKey();
				if("LINESTRING ()".equals(dpr.getGeoString())){
//					System.out.println("asdasdasd");
					continue;
				}
				Geometry g1 = new WKTReader().read(dpr.getGeoString());
				Geometry buffer = g1.buffer(bufferLength);
				point_dirToBufferMap.put(key, buffer);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return point_dirToBufferMap;
	}

}
